﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ykglxt
{
    public partial class Form_MMXG : Form
    {
        public Form_MMXG()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form_MMXG_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                e.Handled = true;
                SendKeys.Send("{TAB}");
            }
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            try
            {
                using (SqlConnection con = new SqlConnection(PublicUnit.ConnetionString))
                {
                    con.Open();
                    SqlCommand com = new SqlCommand("SELECT * FROM Dic_RY WHERE BM = '" + PublicUnit.UserID + "' AND PWD = @PWD " ,con);
                    com.Parameters.Add("@PWD", SqlDbType.VarChar, 10);
                    com.Parameters["@PWD"].Value = tbxOld.Text;
                    SqlDataReader sdr = com.ExecuteReader();
                    if (!sdr.Read())
                    {
                        lblWarning.Text = "原密码有误！";
                        lblWarning.Visible = true;
                        sdr.Close();
                        return;
                    }
                    lblWarning.Visible = false;
                    if (tbxNew1.Text != tbxNew2.Text)
                    {
                        lblWarning.Text = "新密码前后不一致！";
                        lblWarning.Visible = true;
                    }
                    else
                    {
                        sdr.Close();
                        com.CommandText = "UPDATE Dic_RY SET PWD = @PWD WHERE BM = '" + PublicUnit.UserID + "'";
                        com.Parameters["@PWD"].Value = tbxNew2.Text;
                        if (com.ExecuteNonQuery() == 1)
                        {
                            MessageBox.Show("修改成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            tbxOld.Clear();
                            tbxNew1.Clear();
                            tbxNew2.Clear();
                        }
                    }
                    con.Close();
                    sdr.Close();
                }
            }
            catch(SqlException)
            {
                MessageBox.Show("网络故障，请稍后再试！","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
        }

        private void tbxOld_TextChanged(object sender, EventArgs e)
        {
            lblWarning.Visible = false;
        }

        private void tbxNew1_TextChanged(object sender, EventArgs e)
        {
            lblWarning.Visible = false;
        }

        private void tbxNew2_TextChanged(object sender, EventArgs e)
        {
            lblWarning.Visible = false;
        }
    }
}
